﻿using UnityEngine;
using System.Collections;
using UnityEngine.Experimental.Networking;
using System;
namespace yoyo
{
    public static class NetInstrument
    {
        private static GameObject obj;
        private static WWWTools www;

        /// <summary>
        /// 开辟一个新线程来处理网络 post
        /// </summary>
        /// <param name="path">请求地址</param>
        /// <param name="json">请求数据</param>
        public static void Post(string path, WWWForm json, Action<UnityWebRequest> act)
        {
            if (obj == null)
            {
                obj = GameObject.Instantiate(Resources.Load<GameObject>("UI/GameObject"));
                obj.name = "NetInstrument";
                www = obj.AddComponent<WWWTools>();
            }
            www.WWWPost(path, json, act);

        }

       /// <summary>
       /// 开辟一个新线程来处理网络 Get
       /// </summary>
       /// <param name="path">地址</param>
       /// <param name="act">回调</param>
        public static void Get(string path ,Action<UnityWebRequest> act)
        {
            if (obj == null)
            {
                obj = GameObject.Instantiate(Resources.Load<GameObject>("UI/GameObject"));
                obj.name = "NetInstrument";
                www = obj.AddComponent<WWWTools>();
            }
            www.WWWGet(path, act);
        }

        /// <summary>
        /// 开辟一个新线程来处理网络 AB资源
        /// </summary>
        /// <param name="path">地址</param>
        /// <param name="act">回调</param>
        public static void AssetBundle(string path , Action<AssetBundle> act)
        {
            if (obj == null)
            {
                obj = GameObject.Instantiate(Resources.Load<GameObject>("UI/GameObject"));
                obj.name = "NetInstrument";
                www = obj.AddComponent<WWWTools>();
            }
            www.WWWAssetBundle(path, act);
        }

        /// <summary>
        /// 开辟一个新线程来处理网络 图片资源
        /// </summary>
        /// <param name="path">地址</param>
        /// <param name="act">回调</param>
        public static void Texture2D(string path ,Action<Texture2D> act)
        {
            if (obj == null)
            {
                obj = GameObject.Instantiate(Resources.Load<GameObject>("UI/GameObject"));
                obj.name = "NetInstrument";
                www = obj.AddComponent<WWWTools>();
            }
            www.WWWGetTexuture2D(path, act);
        }
    }
}